﻿using CommonLib;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleAppConsumer;

internal class TestDeadQueue
{
    private static readonly string normalExchange = "normal_exchange";
    private static readonly string normalQueue = "normal_queue";
    private static readonly string deadlExchange = "dead_exchange";
    private static readonly string deadlQueue = "dead_queue";

    public static void TestTTL()
    {
        // 声明信道 channel
        var channel = RabbitMQUtil.CreateChannel();

        // 声明普通交换机和死信交换机
        channel.ExchangeDeclare(normalExchange, "direct");
        channel.ExchangeDeclare(deadlExchange, "direct");

        //声明普通和死信队列
        var args = new Dictionary<string, object>();
        args.Add("x-dead-letter-exchange",deadlExchange);
        args.Add("x-dead-letter-routing-key", "lisi");
        channel.QueueDeclare(normalQueue, false, false, false, args);
        channel.QueueDeclare(deadlQueue, false, false, false, null);

        // 绑定交换机和队列
        channel.QueueBind(normalQueue, normalExchange, "zhangsan");
        channel.QueueBind(deadlQueue, deadlExchange, "lisi");

        // 3 声明回调方法
        var consumer = new AsyncEventingBasicConsumer(channel);
        consumer.Received += Consumer_Received;
        async Task Consumer_Received(object sender, BasicDeliverEventArgs @event)
        {
            //Console.WriteLine(sender);
            //var channel = sender as IModel;
            try
            {
                var body = @event.Body.ToArray();
                var msg = Encoding.UTF8.GetString(body);
                Console.WriteLine("Consumer1 收到了消息" + msg);
                channel?.BasicAck(@event.DeliveryTag, false);
                await Task.Delay(10000);
            }
            catch (Exception ex)
            {
                channel?.BasicReject(@event.DeliveryTag, true);
                Console.WriteLine("处理消息：" + ex.Message);
            }
        }

        // 4 消费消息
        channel.BasicConsume(queue: normalQueue, autoAck: false, consumer: consumer);
        Console.ReadLine();
    }

    /// <summary>
    /// 测试队列达到最大长度
    /// 注意：需要把上次创建的队列删除
    /// </summary>
    public static void TestMaxQueue()
    {
        // 声明信道 channel
        var channel = RabbitMQUtil.CreateChannel();

        // 声明普通交换机和死信交换机
        channel.ExchangeDeclare(normalExchange, "direct");
        channel.ExchangeDeclare(deadlExchange, "direct");

        //声明普通和死信队列
        var args = new Dictionary<string, object>();
        args.Add("x-dead-letter-exchange", deadlExchange);
        args.Add("x-dead-letter-routing-key", "lisi");
        args.Add("x-max-length", 6);
        channel.QueueDeclare(normalQueue, false, false, false, args);
        channel.QueueDeclare(deadlQueue, false, false, false, null);

        // 绑定交换机和队列
        channel.QueueBind(normalQueue, normalExchange, "zhangsan");
        channel.QueueBind(deadlQueue, deadlExchange, "lisi");

        // 3 声明回调方法
        var consumer = new AsyncEventingBasicConsumer(channel);
        consumer.Received += Consumer_Received;
        async Task Consumer_Received(object sender, BasicDeliverEventArgs @event)
        {
            //Console.WriteLine(sender);
            //var channel = sender as IModel;
            try
            {
                var body = @event.Body.ToArray();
                var msg = Encoding.UTF8.GetString(body);
                Console.WriteLine("Consumer1 收到了消息" + msg);
                channel?.BasicAck(@event.DeliveryTag, false);
                await Task.Delay(1000);
            }
            catch (Exception ex)
            {
                channel?.BasicReject(@event.DeliveryTag, true);
                Console.WriteLine("处理消息：" + ex.Message);
            }
        }

        // 4 消费消息
        channel.BasicConsume(queue: normalQueue, autoAck: false, consumer: consumer);
        Console.ReadLine();
    }

    public static void TestRejectMsg()
    {
        // 声明信道 channel
        var channel = RabbitMQUtil.CreateChannel();

        // 声明普通交换机和死信交换机
        channel.ExchangeDeclare(normalExchange, "direct");
        channel.ExchangeDeclare(deadlExchange, "direct");

        //声明普通和死信队列
        var args = new Dictionary<string, object>();
        args.Add("x-dead-letter-exchange", deadlExchange);
        args.Add("x-dead-letter-routing-key", "lisi");
        channel.QueueDeclare(normalQueue, false, false, false, args);
        channel.QueueDeclare(deadlQueue, false, false, false, null);

        // 绑定交换机和队列
        channel.QueueBind(normalQueue, normalExchange, "zhangsan");
        channel.QueueBind(deadlQueue, deadlExchange, "lisi");

        // 3 声明回调方法
        var consumer = new AsyncEventingBasicConsumer(channel);
        consumer.Received += Consumer_Received;
        async Task Consumer_Received(object sender, BasicDeliverEventArgs @event)
        {
            //Console.WriteLine(sender);
            //var channel = sender as IModel;
            try
            {
                var body = @event.Body.ToArray();
                var msg = Encoding.UTF8.GetString(body);
                if (msg.Contains("5"))
                {
                    // 第二个参数requeue设置为false,代表拒绝重新入队,也就是该队列如果配置了死信交换机将发送到死信队列中
                    channel?.BasicReject(@event.DeliveryTag, false);
                }
                else
                {
                    Console.WriteLine("Consumer1 收到了消息" + msg);
                    channel?.BasicAck(@event.DeliveryTag, false);
                }
                await Task.Delay(1000);
            }
            catch (Exception ex)
            {
                channel?.BasicReject(@event.DeliveryTag, true);
                Console.WriteLine("处理消息：" + ex.Message);
            }
        }

        // 4 消费消息
        channel.BasicConsume(queue: normalQueue, autoAck: false, consumer: consumer);
        Console.ReadLine();
    }
}
